//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.views.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import ciicos.views.entity.ViewsScriptEntity;
import ciicos.views.model.viewsscript.ViewsScriptForm;
import ciicos.views.model.viewsscript.ViewsScriptInfoVO;
import ciicos.views.model.viewsscript.ViewsScriptListVO;
import ciicos.views.model.viewsscript.ViewsScriptPagination;
import ciicos.views.service.ViewsScriptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"脚本管理"},
    value = "scriptManage"
)
@RequestMapping({"/api/scriptManage/ViewsScript"})
public class ViewsScriptController {
    private static final Logger log = LoggerFactory.getLogger(ViewsScriptController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private ViewsScriptService viewsScriptService;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody ViewsScriptPagination viewsScriptPagination) throws IOException {
        List<ViewsScriptEntity> list = this.viewsScriptService.getList(viewsScriptPagination);
        List<ViewsScriptListVO> listVO = JsonUtil.getJsonToList(list, ViewsScriptListVO.class);

        for(ViewsScriptListVO viewsScriptVO : listVO) {
            viewsScriptVO.setName_name(viewsScriptVO.getName());
            viewsScriptVO.setValue_name(viewsScriptVO.getValue());
            viewsScriptVO.setCreatoruserid_name(this.generaterSwapUtil.userSelectValue(viewsScriptVO.getCreatoruserid()));
            viewsScriptVO.setCreatortime_name(viewsScriptVO.getCreatortime());
            viewsScriptVO.setLastmodifyuserid_name(this.generaterSwapUtil.userSelectValue(viewsScriptVO.getLastmodifyuserid()));
            viewsScriptVO.setLastmodifytime_name(viewsScriptVO.getLastmodifytime());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(viewsScriptPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ViewsScriptForm viewsScriptForm) throws DataException {
        String b = this.viewsScriptService.checkForm(viewsScriptForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            viewsScriptForm.setCreatoruserid(userInfo.getUserId());
            viewsScriptForm.setCreatortime(DateUtil.getNow());
            ViewsScriptEntity entity = (ViewsScriptEntity)JsonUtil.getJsonToBean(viewsScriptForm, ViewsScriptEntity.class);
            entity.setId(mainId);
            this.viewsScriptService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<ViewsScriptInfoVO> info(@PathVariable("id") String id) {
        ViewsScriptEntity entity = this.viewsScriptService.getInfo(id);
        ViewsScriptInfoVO vo = (ViewsScriptInfoVO)JsonUtil.getJsonToBean(entity, ViewsScriptInfoVO.class);
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        if (vo.getCreatortime() != null) {
            vo.setCreatortime(vo.getCreatortime());
        }

        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        if (vo.getLastmodifytime() != null) {
            vo.setLastmodifytime(vo.getLastmodifytime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<ViewsScriptInfoVO> detailInfo(@PathVariable("id") String id) {
        ViewsScriptEntity entity = this.viewsScriptService.getInfo(id);
        ViewsScriptInfoVO vo = (ViewsScriptInfoVO)JsonUtil.getJsonToBean(entity, ViewsScriptInfoVO.class);
        vo.setName(vo.getName());
        vo.setValue(vo.getValue());
        vo.setEnabledmark(this.generaterSwapUtil.switchSelectValue(vo.getEnabledmark(), "启用", "禁用"));
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        vo.setCreatortime(vo.getCreatortime());
        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        vo.setLastmodifytime(vo.getLastmodifytime());
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ViewsScriptForm viewsScriptForm) throws DataException {
        String b = this.viewsScriptService.checkForm(viewsScriptForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            UserInfo userInfo = this.userProvider.get();
            ViewsScriptEntity entity = this.viewsScriptService.getInfo(id);
            if (entity != null) {
                viewsScriptForm.setLastmodifyuserid(userInfo.getUserId());
                viewsScriptForm.setLastmodifytime(DateUtil.getNow());
                ViewsScriptEntity subentity = (ViewsScriptEntity)JsonUtil.getJsonToBean(viewsScriptForm, ViewsScriptEntity.class);
                subentity.setCreatoruserid(entity.getCreatoruserid());
                subentity.setCreatortime(entity.getCreatortime());
                boolean b1 = this.viewsScriptService.updateById(subentity);
                return !b1 ? ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据") : ActionResult.success("更新成功");
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ViewsScriptEntity entity = this.viewsScriptService.getInfo(id);
        if (entity != null) {
            this.viewsScriptService.delete(entity);
        }

        return ActionResult.success("删除成功");
    }
}
